<?php header('Content-type: text/javascript');?>
jQuery(function($){
    $url = "<?php echo $_GET['url_home'];?>/";
    $url_css = "<?php echo $_GET['url_css'];?>/";
    // The option for slider on Shop and Single Product
    var options = {
        effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
        slices: 4, // For slice animations
        boxCols: 4, // For box animations
        boxRows: 4, // For box animations
        animSpeed: 500, // Slide transition speed
        pauseTime: 5000, // How long each slide will show
        directionNav: true, // Next & Prev navigation
        directionNavHide: true, // Only show on hover
        controlNav: true, // 1,2,3... navigation
        controlNavThumbs: true // Use thumbnails for Control Nav
    };

    function ajax_get_img_thumbs(post_id, $this){
        // hiển thị hình loading
        var str = $("<img src='"+$url_css+"/img/loading9.gif'/>");
        $this.append(str);
        $.post($url, { get_type: 'ajax', post_id: post_id, action: 'get_image_thumbs'}, function(result){
            // Xóa hình loading
            $this.children('img').remove();
            var slider = $('.modal-slider');
            // gắn vào modal-slider
            // slider.empty();
            // slider.html($this.siblings('.img-slide-container').html());
            slider.html(result);

            // append product information to modal-box
            $('#slider-info').html($this.siblings('.info-slide-container').html());

            // Open fancybox
            $.fancybox.open($('#modal-box'), {
                closeBtn: false,
                padding: 0,
                beforeShow: function(){
                    // Reset button add_to_Cart and create new slider
                    $('.add_to_cart_btn').removeClass('add_to_cart_btn_clicked').text('Thêm vào giỏ hàng');
                    slider.nivoSlider(options);
                },
                beforeClose: function(){
                    slider.html('');
                    slider.data('nivoslider').stop();
                    $('.list-image').html('');
                }
            });
        });
    }

    // Show product as popup
    $('#list-product').on('click', '.quick-view', function(){
        var $this = $(this);
        ajax_get_img_thumbs($this.data('post-id'), $this);
    });

    // Ajax add to cart
    $('.add_to_cart_btn').on('click', function(){
        var $this = $(this);
        var url = $this.closest('.tooltip').next().find('form').attr('action');

        $.post(url, {quantity: 1}, function(result){
            $this.addClass('add_to_cart_btn_clicked');
            console.log(result);
            $this.text("Đã thêm!");
        });
    });


	$('.nivoSlider').nivoSlider(options);

    // Slide on home page
    /*$('#slider').nivoSlider({
        "effect" : "fade",
        directionNav: false, // Next & Prev navigation
        directionNavHide: false, // Only show on hover
        controlNav: true, // 1,2,3... navigation
        controlNavThumbs: false // Use thumbnails for Control Nav
    });*/

    // Slider on news

    $('#news-slider').easySlider({
        loop: true,                           // Looping
        orientation: 'fade',                  // Fading
        autoplayDuration: 5000,               // Autoplay with 1 second intervals
        autogeneratePagination: true,         // Automatically generate pagination links
        restartDuration: 0,                // In case of user interaction, restart the autoplay after 2.5 seconds
        nextId: 'next',
        prevId: 'prev',
        pauseable: true                       // Pause by hovering over the image!  Then restart after 2.5 seconds (see above)
    });

    // xem với kích thước thật

    $('.open_fc').on('click', function(){
        // Lấy đường dẫn hình vì cái nivoslider nó thêm thuộc tính visibility = hidden
        var arr = [];
        var imgs = $('.fc_box2');
        imgs.each(function(){
            arr.push({
                href: this.src
            });
        });

        // Open fancybox
        $.fancybox.open(arr, {
            closeBtn: false
        });
        
    });
});